package cn.tedu.pet.product.service.impl;

import cn.tedu.pet.commons.ex.ServiceException;
import cn.tedu.pet.product.mapper.BreedMapper;
import cn.tedu.pet.product.mapper.ClientMapper;
import cn.tedu.pet.product.mapper.PetMapper;
import cn.tedu.pet.product.pojo.entity.Client;
import cn.tedu.pet.product.pojo.entity.Pet;
import cn.tedu.pet.product.pojo.param.PetAddNewParam;
import cn.tedu.pet.product.pojo.param.PetUpdateParam;
import cn.tedu.pet.commons.pojo.vo.PageData;
import cn.tedu.pet.product.pojo.vo.PetListItemVO;
import cn.tedu.pet.product.pojo.vo.PetStandardVO;
import cn.tedu.pet.product.service.IPetService;
import cn.tedu.pet.commons.util.PageInfoToPageDataConverter;
import cn.tedu.pet.commons.web.ServiceCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class PetServiceImpl implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private BreedMapper breedMapper;

    @Override
    public void addNew(PetAddNewParam petAddNewParam) {
        log.debug("开始处理[添加宠物]的业务,参数:{}", petAddNewParam);
        Pet petMessage = new Pet();
        BeanUtils.copyProperties(petAddNewParam, petMessage);

        petMessage.setGmtCreate(LocalDateTime.now());
        petMessage.setGmtModified(LocalDateTime.now());
        log.debug("准备将新的宠物数据写入到数据库,数据:{}", petMessage);
        int rows = petMapper.insert(petMessage);
        log.debug("将新的宠物数据写入到数据库,完成!");


    }



    @Override
    public void delete(Long id) {
        log.debug("开始处理[删除宠物数据]的业务,参数:{}",id);
        QueryWrapper<Pet> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int countById=petMapper.selectCount(queryWrapper);
        log.debug("根据宠物ID统计匹配的宠物数量,结果:{}",countById);
        if (countById==0){
            String message="删除宠物数据失败,ID不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }


        // 检查是否有用户关联到了此宠物，如果存在，则抛出异常
        QueryWrapper<Client> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("pet_id",id);
        int countByPetId=clientMapper.selectCount(queryWrapper1);
        log.debug("根据宠物ID统计匹配的用户数量，结果：{}", countByPetId);
        if (countByPetId>0){
            String message="删除宠物数据失败,仍有用户关联到此宠物!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        petMapper.deleteById(id);

    }

    @Override
    public void updateInfoById(Long id, PetUpdateParam petMessageUpdateParam) {
        log.debug("开始处理[修改宠物数据]的业务,ID: {},新数据",id,petMessageUpdateParam);
        QueryWrapper<Pet> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int countById=petMapper.selectCount(queryWrapper);
        log.debug("根据宠物ID统计匹配的宠物数量，结果：{}", countById);
        if (countById==0){
            String message="修改宠物数据失败，宠物数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Pet pet=new Pet();
        BeanUtils.copyProperties(petMessageUpdateParam,pet);
        pet.setId(id);
        int rows=petMapper.updateById(pet);
        if (rows!=1){
            String message = "修改宠物失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.debug("将新的宠物数据更新入到数据库，完成！");
        

    }

    @Override
    public PetStandardVO getStandardById(Long id) {
        PetStandardVO queryResult=petMapper.getStandardById(id);
        if (queryResult==null){
            String message="查询宠物详情失败,宠物数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return queryResult;

    }


    @Override
    public PageData<PetListItemVO> list(Integer pageNum) {
        Integer pageSize=5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<PetListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理[查询宠物列表]的业务,页码:{},每页记录数:{}",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<PetListItemVO> list=petMapper.list();
        PageInfo<PetListItemVO>pageInfo=new PageInfo<>(list);
        PageData<PetListItemVO>pageData= PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}",pageData);
        return pageData;
    }
}
